using System;
using System.Collections.Generic;
using System.IO;

namespace Gloom
{
	public class TranslatedUnmanagedGLFunc
	{
		public string Name;
		public string ReturnType;
		public string[] Parameters;
		
		private TranslatedUnmanagedGLFunc() { }
		
		public static bool TryTranslate(UntranslatedGLFunc u, out TranslatedUnmanagedGLFunc tugf, StreamWriter infoLog, StreamWriter failLog)
		{
			infoLog.WriteLine("TranslatedUnmanagedGLFunc.TryTranslate: Translation begins.");
			
			tugf = new TranslatedUnmanagedGLFunc();
			tugf.Name = "gl" + u.Name;
			tugf.Parameters = new string[u.ParamsNameType.Count];
			
			int i = 0;
			foreach (KeyValuePair<string, string> kvp in u.ParamsNameType)
			{
				// Translate type!
				string newType;
				string[] oldTypeBits = kvp.Value.Split(' ');
				bool b = UnmanagedGLTypeTranslator.TryTranslate(oldTypeBits[0], oldTypeBits[1], oldTypeBits[2], out newType, infoLog, failLog);
				
				if (!b)
					return false;
				
				tugf.Parameters[i] = newType + " " + GLTypePrimitiveMapper.MakeArgNameSafe(kvp.Key);
				i++;
			}
			
			bool br = UnmanagedGLTypeTranslator.TryTranslate(u.ReturnType, "in", "value", out tugf.ReturnType, infoLog, failLog);
			
			return br;
		}
		
		public override string ToString()
		{
			string s = "\t\t[DllImport(\"opengl32.dll\")]\n\t\tinternal static unsafe extern " + ReturnType + " " + Name + "(";
			
			foreach (string param in Parameters)
				s += param.Replace("out ", "[OutAttribute] ") + ", ";
			
			if (Parameters.Length > 0)
				s = s.Substring(0, s.Length - 2);
			
			s += ");\n";
			return s;
		}
	}
}
